<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------


/**
 * 活动页ajax类
 * @package  ajax/backend/content
 * @author   create by allen.qiang
 */

quanli_require('module/base/content/contentCenter.class.php');

class ajax_center {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //查询内容中心信息
                    $this->searchContentCenterList();
                    break;

                case 100:

                    if ( !isset($_REQUEST['act']) || !in_array($_REQUEST['act'], array( 'edit', 'del', 'change') ) ) {

                        returnJsonData('参数错误');

                    }

                    if ($_REQUEST['act'] == 'edit') {

                        $this->saveContentCenter(); //新增、修改活动页内容

                    } else if ($_REQUEST['act'] == 'del') {

                        $this->deleteContent(); //删除活动页

                    } else if ($_REQUEST['act'] == 'change') {

                        $this->setContentStatus(); //启用、禁用活动页

                    }



                    break;

                default:
                    returnJsonData('参数错误');
                    break;

            }

        } else {
            returnJsonData('参数错误');
        }

    }

    /**
     * 查询内容中心信息
     *
     */
    private function searchContentCenterList(){

        global $G_PGLOB;

        $contentCenter = new contentCenter();

        $result_search = $contentCenter->searchContentCenterList( $_REQUEST );

        $preview_url = echo_preview_url();

        $publish_url = echo_publish_url();

        //权限
        $edit_permission = $G_PGLOB->has_permission(1555);

        //组织返回的数据
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ($result_search) {
            foreach($result_search as &$content) {

                $operation = "";

                if(!empty($content['url_link'])){
                    $operation =  '<a href="javascript:void(0);" onclick=content.edit('.$content['contentID'].');>修改</a>&nbsp;&nbsp;'.
                                  '<a href="'.$content['url_link'].'" target="_blank">查看</a>';
                }else{
                    $operation = '<a href="javascript:void(0);" onclick=content.edit('.$content['contentID'].');>修改</a>&nbsp;&nbsp;'.
                                 '<a href="javascript: void(0);" onclick="content.publish(' . $content['contentID'] . ' , \'' . $preview_url . '\')">发布</a>&nbsp;&nbsp;' .
                                 '<a href="javascript: void(0);" onclick="content.preview(' . $content['contentID'] . ', \'' . $preview_url . '\')">预览</a>&nbsp;&nbsp;';
                }

                if ($edit_permission) {

                    $result_array['rows'][] = array(
                        'id' => $content['contentID'],
                        'cell' => array(
                            $operation,
                            //add by allen.qiang 2015-01-13 FUN-BK-0045-PC_WAP首页底下广告位按城市维护 ---start
                            $content['cityName'],
                            //add by allen.qiang 2015-01-13 FUN-BK-0045-PC_WAP首页底下广告位按城市维护 ---end
                            $content['contentID'],
                            $content['contentTitle'],
                            $content['searchKeys'],
                            $content['contentSort'],
                            $content['createTime'],
                            $content['useFlg'] == 1 ? '<a href="#" id="conentStatus_'.$content['contentID'].'" onclick="content.setUse('.$content['contentID'].',1); return false;">启用</a>':'<a href="#" id="conentStatus_'.$content['contentID'].'" onclick="content.setUse('.$content['contentID'].',0); return false;">禁用</a>'
                        )
                    );
                } else {
                    $result_array['rows'][] = array(
                        'id' => $content['contentID'],
                        'cell' => array(
                            //add by allen.qiang 2015-01-13 FUN-BK-0045-PC_WAP首页底下广告位按城市维护 ---start
                            $content['cityName'],
                            //add by allen.qiang 2015-01-13 FUN-BK-0045-PC_WAP首页底下广告位按城市维护 ---end
                            $content['contentID'],
                            '<a href=/shop/display-'.$content['contentID'].'-1.html>'.$content['contentTitle'].'</a>',
                            $content['searchKeys'],
                            $content['contentSort'],
                            $content['createTime'],
                            $content['useFlg'] == 1 ? '启用':'禁用'
                        )
                    );
                }
            }
        }

        echo json_encode($result_array);

    }

    /**
     * 设置内容状态
     *
     */
    private function setContentStatus(){

        global $G_PGLOB;

        $contentCenter = new contentCenter( $G_PGLOB->DBCA );

        $result_array = $contentCenter->setContentStatus( $_REQUEST );

        if($result_array['code'] == 200){

            $result = $G_PGLOB->DBCA->stmt_query('select cityID from content_center where contentID=?','i',$_REQUEST['contentID']);

            $cityID = $result[0]->cityID;

            $this->updateCommContentCenter($cityID);

        }

        echo json_encode($result_array);
    }

    /**
     * 删除内容中心内容
     *
     */
    private function deleteContent(){

        global $G_PGLOB;

        $contentCenter = new contentCenter( $G_PGLOB->DBCA );

        $result_array = $contentCenter->deleteContent( $_REQUEST );

        if($result_array['code'] == 200){

            $cityID = $_REQUEST['content']['cityID'];
            $this->updateCommContentCenter($cityID);

        }

        echo json_encode($result_array);
    }

    /**
     * 保存内容中心
     *
     */
    private function saveContentCenter(){

        global $G_PGLOB;

        $_REQUEST['creator'] = $G_PGLOB->currentUserID;

        $contentCenter = new contentCenter($G_PGLOB->DBCA);

        $result_array = $contentCenter->saveContentCenter( $_REQUEST );

        if($result_array['code'] == 200){

            $cityID = $_REQUEST['content']['cityID'];
            $this->updateCommContentCenter($cityID);

        }

        echo json_encode($result_array);
    }

    /**
     * 保存或修改文章
     *
     */
    private function saveArticle (){

        global $G_PGLOB;

        //实例化文章类
        $contentCenter = new contentCenter($G_PGLOB->DBCA);
        $_REQUEST['creator'] = $G_PGLOB->currentUserID;
        $result_array = $contentCenter -> saveArticleInfo($_REQUEST);

        echo json_encode($result_array);
    }


    /**
     * 按城市更新底部活动页缓存
     * add by allen.qiang 2015-05-26
     */
    private function updateCommContentCenter($cityID=110){

        global $G_PGLOB;

        //实例化文章类
        $contentCenter = new contentCenter($G_PGLOB->DBCA);

        $content_center_array = array();

        $arg_array = array(
            'cityID' => $cityID,
            'searchKeys' => 'foot_content',
            'useFlg' => 1,
            'page' => 1,
            'rp' => 3,
            'sortname' => 'contentSort',
            'sortorder' => 'DESC'
        );

        $temp_content_center_array = $contentCenter->searchContentCenterList( $arg_array );

        if ($temp_content_center_array) {
            foreach($temp_content_center_array as &$content_center) {
                $content_center_array[] = array(
                    'id' => $content_center['contentID'],
                    'title' => $content_center['contentTitle'],
                    'img_link' => $content_center['content_pic_show'],
                    'link' => $content_center['page_link']
                );
            }
            //设置底部活动导航缓存 add by allen.qiang 2015-05-26
            $G_PGLOB->sessionCache_set('comm_content_center_'.$cityID,$content_center_array,0,true);
        }
    }
}